package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.model.UserSessionInfo;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.StaticResource;
import cc.rengu.igas.bsps.facade.request.ApplyImageCodeRequest;
import cc.rengu.igas.bsps.facade.response.ApplyImageCodeResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.ImageCodeResult;
import cc.rengu.oltp.service.realize.ImageCodeService;
import cc.rengu.oltp.service.realize.impl.ImageCodeServiceImpl;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;

/**
 * 申请图片验证码
 * Created by 王成 on 2020/3/26
 */
public class ApplyImageCodeService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        ApplyImageCodeRequest applyImageCodeRequest = new ApplyImageCodeRequest();
        ConvertUtil.convertOutput(applyImageCodeRequest);
        return applyImageCodeRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        ApplyImageCodeRequest applyImageCodeRequest = (ApplyImageCodeRequest) request;
        ApplyImageCodeResponse applyImageCodeResponse = new ApplyImageCodeResponse();
        BeanUtil.beanCopy(applyImageCodeRequest, applyImageCodeResponse);
        /* 获取用户SESSION会话信息 */
        String userSessionString;
        if (StringUtil.isEmptyOrNull(applyImageCodeRequest.getHeader().getSession()) || StringUtil.isEmptyOrNull(userSessionString = RedisUtil.onceGet(applyImageCodeRequest.getHeader().getSession()))) {
            rglog.error("用户未登录或者Session已过期!");
            throw new BizException(RespCodeEnum.USER_NOT_LOGIN_OR_SESSION_IS_EXPIRED.getRespCode(), RespCodeEnum.USER_NOT_LOGIN_OR_SESSION_IS_EXPIRED.getRespDesc());
        }
        /* 调用图片验证码服务器获取图型验证码静态资源 */
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        ImageCodeService imageCodeService = new ImageCodeServiceImpl();
        ImageCodeResult imageCodeResult = imageCodeService.applyImageCode(applyImageCodeRequest.getHeader().getInstId(), sysSeqNum, null);
        if (null == imageCodeResult || null == imageCodeResult.getResult()) {
            rglog.error("生成图片验证码失败!");
            throw new BizException(RespCodeEnum.IMAGE_CODE_APPLY_ERROR.getRespCode(), RespCodeEnum.IMAGE_CODE_APPLY_ERROR.getRespDesc());
        } else {
            if ("local".equals(imageCodeResult.getImageType())) {
                applyImageCodeResponse.setAuthCodeImage((String) imageCodeResult.getResult());
            } else {
                StaticResource staticResource = new StaticResource();
                BeanUtil.beanCopy(imageCodeResult.getResult(), staticResource);
                applyImageCodeResponse.setStaticResource(staticResource);
            }
        }
        /* 将图片验证码存储到Redis缓存中 */
        UserSessionInfo userSessionInfo = JSON.parseObject(userSessionString, UserSessionInfo.class);
        userSessionInfo.setImageCode(RedisUtil.hashGet(AppParamConstant.IMAGE_CODE_CACHE, sysSeqNum));
        String userSessionKey = applyImageCodeRequest.getHeader().getSession();
        RedisUtil.onceSet(userSessionKey, JSON.toJSONString(userSessionInfo), BspsAppParamConstant.SESSION_EFFECTIVE_TIME);
        /* 组装应答报文 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(applyImageCodeResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ApplyImageCodeResponse applyImageCodeResponse = (ApplyImageCodeResponse) bizResponse.getResult();
        ConvertUtil.convertInput(applyImageCodeResponse);
    }
}
